package com.parttimetask.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.parttimetask.App;
import com.parttimetask.activities.main.MainActivity;
import com.parttimetask.databinding.ActivityLoginBinding;
import com.parttimetask.utils.Http;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.sign.setOnClickListener(view -> startActivityForResult(new Intent(LoginActivity.this,SignActivity.class),2));
        binding.tAc.setOnClickListener(view -> {
            startActivity(new Intent(LoginActivity.this,PrivacyAgreementActivity.class));
        });
        binding.loginLogin.setOnClickListener(view -> {
            if (binding.loginMobile.getText().toString().equals("")|| binding.loginPassword.getText().toString().equals("")){
                Toast.makeText(LoginActivity.this, "Please input completely", Toast.LENGTH_SHORT).show();return;
            }
            Http.get("user/login/?mobile="+binding.loginMobile.getText().toString()+"&password="+binding.loginPassword.getText().toString(), new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            String res = response.body().string();
                            if (res.equals("验证成功")){
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                                edit.putString("mobile",binding.loginMobile.getText().toString());
                                edit.putString("password",binding.loginPassword.getText().toString());
                                edit.apply();
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                }
            });
        });
        Http.get("getMobileArea/", new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
            }
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        binding.loginNumberCode.setText("+"+response.body().string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        });
        autoLogin();
    }

    private void autoLogin() {
        String mobile = App.sp.getString("mobile", "null");
        String password = App.sp.getString("password", "null");
        if (!mobile.equals("null")&&!password.equals("null")){
            Http.get("user/login/?mobile="+mobile+"&password="+password, new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            String res = response.body().string();
                            if (res.equals("验证成功")){
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                }
            });

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==2&&resultCode==5){
            autoLogin();
        }
    }
}
